/**
 * Created by wrl on 2017/7/13.
 */
var path = '../';
Vue.http.options.emulateJSON = true;
$(function(){
    new Vue({
        el: '#container',
        data: {
            action:util.getUrlParams()['action'],
            updateId:util.getUrlParams()['updateId'],
            personInfo:{},
            roleList:[],
            checkAccountPwd:null,
            checkPwdRuleErr:""
        },
        mounted: function () {
            var self = this;
            self.$http.get(path+'role/list',{}).then(function(response){
                var list = response.body
                if(list && list.length != 0){
                    self.roleList = list
                    //设置默认选中第一项
                    self.personInfo.roleCode = list[0].roleCode
                }
            }, function (response) {
                console.log('request err..')
            })

            if(self.action == 'update' && self.updateId != null){
                self.$http.get(path+'personInfo/updateInit/'+self.updateId,{}).then(function(response){
                    self.personInfo = response.body;
                },function(res){
                    console.log(res.body)
                })
            }
        },
        methods: {
            save:function(){
                var self = this;
                self.checkPwdRuleErr='';
                if(checkNotEmpty()){
                    if(self.checkAccountPwd != self.personInfo.accountPwd)  return;
                    var regex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z]).{6,16}');
                    if (!regex.test(self.personInfo.accountPwd)){
                        self.checkPwdRuleErr='密码中必须包含字母、数字，至少6个字符，最多16个字符';
                        return;
                    }
                    if(self.personInfo.accountPwd == self.personInfo.accountCode){
                        self.checkPwdRuleErr='密码不能与账户名相同，请重新输入！';
                        return;
                    }
                    self.personInfo.registerTime = new Date(self.personInfo.registerTime)
                    self.personInfo.birthday = new Date(self.personInfo.birthday)
                    self.$http.post(path+'personInfo/save',self.personInfo).then(function(response){
                        if(response.body.flag == true){
                            tips('添加成功!',1);
                            setTimeout(function(){
                                parent.window.location.replace(parent.window.location.href);
                            },500);
                        }else{
                            if(response.body.sysErrorMsg)
                                tips(response.body.sysErrorMsg,5);
                            if(response.body.failMsg)
                                tips(response.body.failMsg,5);
                        }
                    },function(res){
                        console.log(res.body);
                    })
                }
            },
            update:function(){
                var self = this;
                if(checkNotEmpty()){
                    self.personInfo.registerTime = new Date(self.personInfo.registerTime)
                    self.personInfo.birthday = new Date(self.personInfo.birthday)
                    self.$http.post(path+'personInfo/update',self.personInfo).then(function(response){
                        if(response.body.flag == true){
                            tips('更新成功!',1);
                            setTimeout(function(){
                                parent.window.location.replace(parent.window.location.href);
                            },500);
                        }else{
                            if(response.body.sysErrorMsg)
                                tips(response.body.sysErrorMsg,5);
                            if(response.body.failMsg)
                                tips(response.body.failMsg,5);
                        }
                    },function(res){
                        console.log(res.body);
                    })
                }
            }
        }
    })

    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });

    $("#container").Validform({
        tiptype:2,
        callback:function(form){
            form[0].submit();
            var index = parent.layer.getFrameIndex(window.name);
            parent.$('.btn-refresh').click();
            parent.layer.close(index);
        }
    });
});

/**
 * 校验input是否为空
 * @returns {boolean}
 */
function checkNotEmpty(){
    var flag = true;
    $('.input-text:not(".noInput")').each(function(index,ele){
        if($(this).val() == ''){
            $(this).focus().blur();
            flag = false;
            return true;
        }
    });
    return flag;
}